package com.xzzz.irda.guardcat.core.metric.rocketmq;

import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 *
 * @author xzzz
 */
public class RocketMQConsumerBeanPostProcessor implements BeanPostProcessor {

    @Autowired(required = false)
    public RocketMQMetricManager rocketMQMetricManager;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        try {
            RocketMQMessageListener rocketMQMessageListener =
                    AnnotationUtil.getAnnotation(bean.getClass(), RocketMQMessageListener.class);
            if (rocketMQMessageListener != null) {
                String topic = rocketMQMessageListener.topic();
                if (StrUtil.isNotBlank(topic) && rocketMQMetricManager != null) {
                    rocketMQMetricManager.addConsumerTopicName(topic);
                }
            }
        } catch (Throwable ignored) {
        }
        return bean;
    }
}
